#ifndef RENDELES_H
#define RENDELES_H

class Rendeles
{
        int _id;
        Felhasznalo _felhasznalo;
        Lista<Tetel*>* _tetelek;
        Lista<Tetel*> _kosar;
        std::set<std::string>* _kategoriak;
        char _postazas;
        bool _torol;
    public:
        Rendeles(const int id, const Felhasznalo& f, Lista<Tetel*>* tetelek, bool l = false, std::set<std::string>* kategoriak = NULL)
        {
            _id = id;
            _felhasznalo = f;
            _tetelek = tetelek;
            _kategoriak = kategoriak;
            _torol = false;
            _postazas = 'h';
            if (!l)
            {
                Edit();
            }
        }
        Rendeles(const Rendeles& rhs)
        {
            _id = rhs._id;
            _felhasznalo = rhs._felhasznalo;
            _kosar = rhs._kosar;
            _postazas = rhs._postazas;
            _tetelek = rhs._tetelek;
            _kategoriak = rhs._kategoriak;
            _torol = false;
        }
        /*operator Rendeles*()
        {
            return this;
        }*/
        Lista<Tetel*>* GetKosar()
        {
            return &_kosar;
        }
        Felhasznalo& GetFelhasznalo()
        {
            return _felhasznalo;
        }
        void Edit(bool l = false)
        {
            std::string s = "";
            while (SelectFunction(s)>1)
            {
                std::cout << std::endl << "=== Rendeles ===" << std::endl;
                Iterator<Tetel*> it =  _kosar.Begin();
                if (_kosar.Size() == 0)
                {
                    std::cout << "A kosar ures." << std::endl;
                }
                else
                {
                    std::cout << "A kosar tartalma:" << std::endl;
                }
                int i = 1;
                while (it != _kosar.End())
                {
                    Tetel t = *(*it);
                    std::cout << i << " - Nev: " << t.GetName() << ";"
                        << " Kategoria: " << t.GetCategory()  << "; Egysegar: " << t.GetValue()  << "; Darabszam: " << t.GetQuantity() << ";";
                        if (t.GetT() == "sz")
                        {
                            std::cout << std::endl << "Megjegyzes: " << (*it)->GetComment();
                        }
                        std::cout << std::endl;
                    ++it;
                    ++i;
                }
                std::cout << "Atvetel: " << _postazas << std::endl;
                std::cout << "Teljes osszeg: " << GetValue() << std::endl;
                std::cout << "================" << std::endl << "1. Tetel hozzaadasa" << std::endl;
                std::cout << "2. Tetel torlese" << std::endl;
                std::cout << "3. Atvetel kivalasztasa" << std::endl;
                std::cout << "4. Rendeles leadasa" << std::endl;
                std::cout << "0. Vissza a menube (Rendeles torlese)" << std::endl;
                std::cout << "Valasztas (1/2/3/4/0): ";
                std::cin >> s;
            }
        }
        void AddTetel(Tetel* t)
        {
            _kosar.InsertLast(t);
        }
        char GetPostazas()
        {
            return _postazas;
        }
        void SetPostazas(char postazas)
        {
            _postazas = postazas;
        }
        int GetValue()
        {
            int r = 0;
            Iterator<Tetel*> it =  _kosar.Begin();
            while (it != _kosar.End())
            {
                r += (*(*it)).GetValue()*(*(*it)).GetQuantity();
                ++it;
            }
            return r;
        }
        int SelectFunction(std::string f)
        {
            if (f == "0")
            {
                _torol = true;
                return 0;
            }
            else if (f == "1")
            {
                std::cout << "================" << std::endl  << "Kategoria lista:" << std::endl;
                int i=1;
                for (std::set<std::string>::iterator it=_kategoriak->begin(); it!=_kategoriak->end(); ++it)
                {
                    std::cout << i << " - " << *it << std::endl;
                    ++i;
                }
                int j=i;
                while (j<1 || j>=i)
                {
                    std::cout << "Valasszon kategoriat az index beirasaval: ";
                    std::cin >> j;
                }
                std::set<std::string>::iterator sit=_kategoriak->begin();
                while(j>1)
                {
                    ++sit;
                    --j;
                }
                std::cout << "================" << std::endl  << "Tetel lista:" << std::endl;
                Iterator<Tetel*> it = _tetelek->Begin();
                i=1;
                while (it != _tetelek->End())
                {
                    Tetel t = *(*it);
                    if (t.GetCategory() == *sit)
                    {
                        std::cout << i <<  " - Nev: " << t.GetName() << "; Egysegar: " << t.GetValue()  << "; Darabszam: " << t.GetQuantity();
                        std::cout << std::endl;
                        ++i;
                    }
                    ++it;
                }
                j=i;
                while (j<1 || j>=i)
                {
                    std::cout << "Valasszon tetelt az index beirasaval: ";
                    std::cin >> j;
                }
                it = _tetelek->REnd();
                int tm = 0;
                while(tm != j)
                {
                    ++it;
                    if ((*it)->GetCategory() == (*sit)) ++tm;
                }
                if ((*it)->GetT() == "t")
                {
                    int db=(*it)->GetQuantity()+1;
                    while (db<0 || db>(*it)->GetQuantity())
                    {
                        std::cout << "Darabszam (irjon 0-t visszavonashoz): ";
                        std::cin >> db;
                    }
                    if (db > 0)
                    {
                        Iterator<Tetel*> ttit = _kosar.Begin();
                        while (ttit != _kosar.End() && ((*ttit)->GetName() != (*it)->GetName()))
                        {
                            ++ttit;
                        }
                        (*it)->SetQuantity((*it)->GetQuantity()-db);
                        if(ttit == _kosar.End())
                        {
                            Tetel* tmp = new Termek(*(*it));
                            tmp->SetQuantity(db);
                            _kosar.InsertLast(tmp);
                        }
                        else
                        {
                            (*ttit)->SetQuantity((*ttit)->GetQuantity()+db);
                        }
                    }
                }
                else if ((*it)->GetT() == "sz")
                {
                    int db=(*it)->GetQuantity()+1;
                    while (db<0 || db>(*it)->GetQuantity())
                    {
                        std::cout << "Darabszam (irjon 0-t visszavonashoz): ";
                        std::cin >> db;
                    }
                    if (db > 0)
                    {
                        std::string com;
                        std::cout << "Megjegyzes (pl. melyik termekekhez lesz): ";
                        std::cin >> com;
                        Tetel* tmp = new Szolgaltatas(*(*it), com);
                        (*it)->SetQuantity((*it)->GetQuantity()-db);
                        tmp->SetQuantity(db);
                        _kosar.InsertLast(tmp);
                    }
                }
            }
            else if (f == "2")
            {
                if (_kosar.Size() > 0)
                {
                    int j=_kosar.Size()+1;
                    while (j<0 || j>_kosar.Size())
                    {
                        std::cout << "Valasszon torlendo tetelt az index beirasaval (irjon 0-t visszavonashoz): ";
                        std::cin >> j;
                    }
                    if (j != 0)
                    {
                        Iterator<Tetel*> it = _kosar.Begin();
                        while(j>1)
                        {
                            ++it;
                            --j;
                        }
                        Iterator<Tetel*> tit = _tetelek->Begin();
                        while ((*tit)->GetName() != (*it)->GetName()) ++tit;
                        (*tit)->SetQuantity((*tit)->GetQuantity()+(*it)->GetQuantity());
                        _kosar.Remove(it);
                    }
                }
            }
            else if (f == "3")
            {
                char p = ' ';
                while (p != 'h' && p != 'p' )
                {
                    std::cout << "Helyben vagy postazva keri? (h/p): ";
                    std::cin >> p;
                }
                SetPostazas(p);
            }
            else if (f == "4")
            {
                return 0;
            }
            return 2;
        }
        int GetID()
        {
            return _id;
        }
        bool GetTorol()
        {
            return _torol;
        }
};

#endif
